﻿@namespace BlazorFluentUI.Lists
@inherits FluentUIComponentBase
@typeparam TItem
@typeparam TKey

<List TItem="IGroupedListItem3<TItem>"
         @ref="listReference"
         ClassName=@($"ms-GroupedList{(string.IsNullOrEmpty(ClassName) ? "" : " " + ClassName)}")
         IsVirtualizing=@IsVirtualizing
         ItemsSource=@ItemsSource
         StartIndex=@StartIndex
         >
    <ItemTemplate>
        @{
            #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
            if (context.Item is HeaderItem3<TItem, TKey>)
            {
                var header = context.Item as HeaderItem3<TItem, TKey>;
                bool isSelected = false;
                if (Selection != null && header != null)
                {
                    isSelected = Selection.IsRangeSelected(header.GroupIndex, header.Count);
                    if (!headerSubscriptions.ContainsKey(header))
                    {
                        var disposable = Selection.SelectionChanged.Subscribe(_ =>
                        {
                            Console.WriteLine("HeaderRangeCheck");
                            var selected = Selection.IsRangeSelected(header.GroupIndex, header.Count);
                            if (selected != isSelected)
                            {
                                isSelected = selected;
                                InvokeAsync(StateHasChanged);
                            }
                        });
                        headerSubscriptions.Add(header, disposable);
                    }
                }
                #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
                    <GroupHeader IsOpen=@header!.IsOpen
                                 OnOpenChanged=@((isOpen) =>
                                                 {
                                                     if (header.IsOpen != isOpen)
                                                     {
                                                         header.IsOpen = isOpen;
                                                         InvokeAsync(StateHasChanged);
                                                     }
                                                 })
                                 Count=@header.Count
                                 GroupLevel=@header.Depth
                                 Name=@header.Name
                                 Compact=@Compact
                                 CurrentlySelected=@isSelected
                                 IsSelectionCheckVisible=@(Selection != null)
                                 OnClick=@(() => OnHeaderClick!(context))
                                 OnToggle=@(() => OnHeaderToggle!(context)) />
                    @if (header.IsOpen)
                    {
                        <GroupItemAuto ItemsSource=@header.Items
                                       TItem=@TItem
                                       TKey=@TKey
                                       GetKey=@(GetKey!)
                                       IsVirtualizing=@IsVirtualizing
                                       Compact=@Compact
                                       ItemTemplate=@(ItemTemplate!)
                                       Selection=@Selection
                                       SelectionMode=@SelectionMode
                                       StartIndex=@header.GroupIndex
                                       OnHeaderClick=@(OnHeaderClick!)
                                       OnHeaderToggle=@(OnHeaderToggle!) />

                    }
            }
            else
            {

                    @ItemTemplate!(context)

            }
        }
    </ItemTemplate>
</List>
